package com.buss.product.utils;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.support.WebApplicationContextUtils;

import com.buss.base.model.DicData;
import com.buss.base.service.DicDataService;
import com.buss.core.model.User;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.ResultList;
import com.buss.core.sysparam.SysStaticParam;

public class ProductHelper {

	/**根据数据字典编码获取数据字典列表信息*/
	public static List<DicData> getDicDataByParentCode(String code,HttpServletRequest request){
		ServletContext application = request.getSession().getServletContext();
		DicDataService dicDataService = WebApplicationContextUtils.getWebApplicationContext(application).getBean("dicDataServiceImpl",DicDataService.class);
		User user = getUser(request);
		OrderBy orderBy = new OrderBy();
		orderBy.add("orderSort", OrderBy.ASC);
		ResultList<DicData> rs = dicDataService.queryDicData(user.getCompanyId(), code, null, orderBy);
		return rs.getData();
	}
	
	
	public static User getUser(HttpServletRequest request){
		return (User)request.getSession().getAttribute(SysStaticParam.SESSION_USER);
	}
	
	public static void test(){
		String[][] a = new String[4][];
		a[0] = new String[]{"1","2","3","4","5"};
		a[1] = new String[]{"a","b","c","d"};
		a[2] = new String[]{"A","B","C"};
		a[3] = new String[]{"A","B","C","9"};
		int s = a.length;
		int[] b = new int[s];
		for(int i = 0;i < s;++i){
			b[i] = 0;
		}
		int index = 0;
		do{
			for(int i = 0;i < s;++i){
				int d = b[i];
				System.out.print(a[i][d]);
			}
			
			System.out.println();
			System.out.println(index);
			index++;
		}while(register(a, b));
	}
	
	private static boolean register(String[][] a,int[] b){
		int s = b.length;
		for(int i = b.length - 1;i >= 0;--i){
			if(b[i] + 1 < a[i].length){
				b[i] = b[i] + 1;
				return true;
			}else if(i - 1 >= 0){
				b[i] = 0;
			}else{
				return false;
			}
		}
		return false; 	
	}
	
	
	public static void main(String[] args){
		String jsonStr = "[{'cssx_ys':'北欧风情蓝','cssx_cm':'38（165/84）'}"+
				"{'cssx_ys':'北欧风情蓝','cssx_cm':'39（170/88）'}";
		
		
		
		test();
	}
}
